package edu.its.dreamteam.web.pages.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.Position;
import edu.its.dreamteam.daf.dao.IPositionDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class PositionGrid extends SecureBasePage{
	@Inject
	private IPositionDao positionDao;
	private List<Position> positionList;
	private Position position;
	
	@Retain
	private BeanModel positionModel;
	@Inject
	private BeanModelSource positionModelSource;
    @Inject
    private ComponentResources resources;
      
    private boolean delete;
    private List<String> deletedList;
        
    
	public boolean isDelete() {
		return delete;
	}
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(getPosition().getIdPosition());
		}
		this.delete = delete;
	}
	public List<String> getDeletedList() {
		if(deletedList==null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}
	public Position getPosition() {
		return position;
	}
	public void setPosition(Position position) {
		this.position = position;
	}
	public IPositionDao getPositionDao() {
		return positionDao;
	}
	public List<Position> getPositionList() {
		if(positionList == null){
			positionList = new ArrayList<Position>();			
		}
		positionList = getPositionDao().queryForList("", "");
		return positionList;
	}
	
	void pageLoaded(){
    	positionModel = positionModelSource.create(Position.class, false, resources);
    	positionModel.add("actions", null).label("Edit");	
    }	
	
	Object onSuccessFromPositionForm(){
		for(String element : getDeletedList()){
			setPosition((Position) getPositionDao().findByPrimaryKey(element));
			getPositionDao().delete(getPosition());
		}
		return PositionGrid.class;
	}
	public BeanModel getPositionModel() {
		return positionModel;
	}
	
	
	
	
}
